﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using Prime.Models;

namespace Prime.Controllers
{
    [Authorize]
    public class LessonMaterialsController : ApiController
    {
        private LearningDbEntities db = new LearningDbEntities();

        // GET api/LessonMaterials
        public IQueryable<LessonMaterial> GetLessonMaterials()
        {
            return db.LessonMaterials;
        }

        // GET api/LessonMaterials/5
        [ResponseType(typeof(LessonMaterial))]
        public IHttpActionResult GetLessonMaterial(int id)
        {
            LessonMaterial lessonmaterial = db.LessonMaterials.Find(id);
            if (lessonmaterial == null)
            {
                return NotFound();
            }

            return Ok(lessonmaterial);
        }

        // PUT api/LessonMaterials/5
        public IHttpActionResult PutLessonMaterial(int id, LessonMaterial lessonmaterial)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != lessonmaterial.LessonId)
            {
                return BadRequest();
            }

            db.Entry(lessonmaterial).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!LessonMaterialExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST api/LessonMaterials
        [ResponseType(typeof(LessonMaterial))]
        public IHttpActionResult PostLessonMaterial(LessonMaterial lessonmaterial)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.LessonMaterials.Add(lessonmaterial);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (LessonMaterialExists(lessonmaterial.LessonId))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = lessonmaterial.LessonId }, lessonmaterial);
        }

        // DELETE api/LessonMaterials/5
        [ResponseType(typeof(LessonMaterial))]
        public IHttpActionResult DeleteLessonMaterial(int id)
        {
            LessonMaterial lessonmaterial = db.LessonMaterials.Find(id);
            if (lessonmaterial == null)
            {
                return NotFound();
            }

            db.LessonMaterials.Remove(lessonmaterial);
            db.SaveChanges();

            return Ok(lessonmaterial);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool LessonMaterialExists(int id)
        {
            return db.LessonMaterials.Count(e => e.LessonId == id) > 0;
        }
    }
}